﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using AteckCsharp.Classes;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Web;
using System.Web.UI;
namespace AteckCsharp.Classes
{
    public class LoadDefaulPage
    {
        String user;
        String WeekNumber;
        public LoadDefaulPage(String userId, String WeekNo)
        {
            this.user = userId;
            this.WeekNumber = WeekNo;
        }
        //private LoadExistingWeek()
        //{

        //}
        public DataSet LoadWeek()
        {

            DataTable dt = new DataTable();
            DAL dal = new DAL();
            DataSet ds = new DataSet();
            int weekNum = -1;
            ds = dal.GetExistingWeek(user, WeekNumber);
            dt = ds.Tables[0];
            if (dt.Rows.Count > 0) //this is an existing week.
            {
                weekNum = dt.Rows[0].Field<int>(4);
            }
            else
            {
                weekNum = int.Parse( WeekNumber);
            }
                //Get the days based on Week Number.
                 NameValueCollection daysList = new NameValueCollection();
                 daysList = GetDaysBasedOnWeekNumber(weekNum);
            //Create a new datatable to bind to grid view.
            DataTable dtGrid = new DataTable();
            int i = daysList.Count -1;
            while( i != -1)
            {
                DateTime daTime;
                string[] dayName;
                dayName = daysList.GetValues(i);
                daTime = DateTime.Parse(dayName[0]);
                string s = daTime.ToString("MM-dd");
                if (daysList.GetKey(i).Equals("Friday"))
                {
                    HttpContext.Current.Session.Remove("dateOfGivingFriday");
                    HttpContext.Current.Session.Add("dateOfGivingFriday", daTime.ToString("MM-dd-yyyy"));
                }
                DataColumn dtCol = new DataColumn(daysList.GetKey(i) + "<br/>" + s);
                dtCol.DefaultValue = daysList.GetKey(i);
                dtGrid.Columns.Add(dtCol);
                i--;
            }
            
         //Load the existing record int the dtGrid.
            //foreach (DataRow dtRow in dt.Rows)
            //{
            //    var newRow = dtGrid.NewRow();
            //    newRow.ItemArray = dtRow.ItemArray.Clone() as object[];
            //    dtGrid.Rows.Add(newRow);
            //}
            //Get the count of the dtGrid. if less than 12. add the missing ones.

            while (dt.Rows.Count < 13)
            {
                dt.Rows.Add(dt.NewRow());
            }
           
            ds.Tables.Add(dtGrid);
            return ds ;
        }
        private System.Collections.Specialized.NameValueCollection GetDates()
        {
            System.Collections.Specialized.NameValueCollection days = new System.Collections.Specialized.NameValueCollection();
            DateTime now = DateTime.Today;
            int today = (int)now.DayOfWeek;
            int[] nextDay = { -1, 0, 1, 2, 3, 4, 5 };
            days = GetNextDates(nextDay, now.DayOfWeek);
            return days;
        }
        private System.Collections.Specialized.NameValueCollection GetNextDates(int[] nDays, System.DayOfWeek today)
        {
            System.Collections.Specialized.NameValueCollection li = new System.Collections.Specialized.NameValueCollection();
            for (int i = 0; i < nDays.Length; i++)
            {
                int find = nDays[i];
                int delta = find - (int)today;
                DateTime da;
                da = DateTime.Now.AddDays(delta);
                li.Add(DateTime.Now.AddDays(delta).DayOfWeek.ToString(), DateTime.Now.AddDays(delta).ToString());
            }
            return li;
        }
        private NameValueCollection GetDaysBasedOnWeekNumber(int weekNo)
        {
            NameValueCollection daysList = new NameValueCollection();
            DateTime jan1 = new DateTime(2014, 1, 1);
            int daysOffset = DayOfWeek.Friday - jan1.DayOfWeek;
            DateTime firstSat = jan1.AddDays(daysOffset);

            var cal = CultureInfo.CurrentCulture.Calendar;
            int firstWeek = cal.GetWeekOfYear(firstSat, CalendarWeekRule.FirstFullWeek, DayOfWeek.Saturday);

            if (firstWeek <= 1)
            {
                weekNo -= 1;
            }

            DateTime result = firstSat.AddDays(weekNo * 7);
            daysList.Add(result.DayOfWeek.ToString(), result.ToShortDateString());
            for (int i = 0; i < 6; i++)
            {
                result = result.AddDays(-1);
                daysList.Add(result.DayOfWeek.ToString(), result.ToShortDateString());
            }
            return daysList;
        }
    }
}